export class UserPlayLevel {
  static LevelNameArray = ['球童', '球士', '球王', '球尊', '球圣', '球帝', '球神'];
  public playLevel = 0;
  constructor(pl: number) {
    this.playLevel = pl;
  }
  clear() {
    this.playLevel = 0;
  }
  setPlayLevel(lv: number) {
    this.playLevel = lv;
  }
  getPlayLevel() {
    if (this.playLevel < 10) { // [0, 10)
      return 0;
    }
    if (this.playLevel < 20) { // [10, 20)
      return 1;
    }
    if (this.playLevel < 30) { // [20, 30)
      return 2;
    }
    if (this.playLevel < 40) { // [30, 40)
      return 3;
    }
    if (this.playLevel < 50) { // [40, 50)
      return 4;
    }
    if (this.playLevel < 60) { // [50, 60)
      return 5;
    }
    if (this.playLevel < 70) { // [60, 70)
      return 6;
    }
    return 6;
  }
  playLevelName() {
    const star = Math.floor((this.playLevel % 10) / 2 + 1);
    if (this.playLevel < 10) { // [0, 10)
      return `${star}星${UserPlayLevel.LevelNameArray[0]}`;
    }
    if (this.playLevel < 20) { // [10, 20)
      return `${star}星${UserPlayLevel.LevelNameArray[1]}`;
    }
    if (this.playLevel < 30) { // [20, 30)
      return `${star}星${UserPlayLevel.LevelNameArray[2]}`;
    }
    if (this.playLevel < 40) { // [30, 40)
      return `${star}星${UserPlayLevel.LevelNameArray[3]}`;
    }
    if (this.playLevel < 50) { // [40, 50)
      return `${star}星${UserPlayLevel.LevelNameArray[4]}`;
    }
    if (this.playLevel < 60) { // [50, 60)
      return `${star}星${UserPlayLevel.LevelNameArray[5]}`;
    }
    if (this.playLevel < 70) { // [60, 70)
      return `${star}星${UserPlayLevel.LevelNameArray[6]}`;
    }
    // >= 500,
    return UserPlayLevel.LevelNameArray[6];
  }
  canMatchBattle(playLevel: number) {
    if (Math.abs(this.playLevel - playLevel) <= 10) {
      return true;
    }
  }
  addLevel(df: number) {
    this.playLevel += df;
    if (this.playLevel < 0) this.playLevel = 0;
  }
  aiPlayLevel() {
    if (this.playLevel < 4) { // [0, 10)
      return 0;
    }
    if (this.playLevel < 20) { // [10, 30)
      return 1;
    }
    if (this.playLevel < 40) { // [40, 90)
      return 2;
    }
    if (this.playLevel < 90) { // [50, 100)
      return 3;
    }
    if (this.playLevel < 150) { // [100, 200)
      return 4;
    }
    if (this.playLevel < 300) { // [200, 450)
      return 5;
    }
    if (this.playLevel < 500) { // [200, 450)
      return 6;
    }
    if (this.playLevel < 800) { // [200, 450)
      return 7;
    }
    if (this.playLevel < 1300) { // [200, 450)
      return 8;
    }
    return 9;
  }

  levelLogoName() {
    const index = Math.floor((this.playLevel / 10));
    return `LevelLogo-${index}`;
  }
}
/**
 * 用户属性类
 */
export class UserAttribute {
  static defaultUserId = '10000000000001';
  public userId = '';
  public nickName = '';
  public avatarUrl = '';
  public noviceTraining = 0;
  public playLevel: UserPlayLevel = null;
  public goldCoin = 0;
  public winNum = 0;
  public loseNum = 0;
  public tieNum = 0;
  public modifyTime = '';
  public createTime = '';
  static newDefaultUser() {
    return new UserAttribute(
      UserAttribute.defaultUserId, '待加入', '', 1, 0,
    );
  }
  static newEmptyUser() {
    return new UserAttribute(
      UserAttribute.defaultUserId, '', '', 1, 0,
    );
  }
  static newPlayLevelUser(lv: number) {
    return new UserAttribute(
      UserAttribute.defaultUserId, '神经娃', '', 1, lv,
    );
  }
  constructor(uid='', name='', url='', t=0, lv=0) {
    this.userId = uid;
    this.nickName = name;
    this.avatarUrl = url;
    this.noviceTraining = t;
    // 每 10 一个大等级
    this.playLevel = new UserPlayLevel(lv);
    this.goldCoin = 1001;
    this.winNum = 100;
    this.loseNum = 100;
    this.tieNum = 10;
    this.modifyTime = '';
    this.createTime = '';
  }
  isRobotUser() {
    return this.userId.startsWith('1000000000');
  }
  clear() {
    this.userId = UserAttribute.defaultUserId;
    this.nickName = '';
    this.avatarUrl = '';
    this.noviceTraining = 0;
    // 每 10 一个大等级
    this.playLevel.clear();
    this.goldCoin = 0;
    this.winNum = 0;
    this.loseNum = 0;
    this.tieNum = 0;
    this.modifyTime = '';
    this.createTime = '';
  }
  setUserInfo(msg: any) {
    if (!msg) return;
    if (msg.hasOwnProperty('userId')) {
      this.userId = msg.userId;
    }
    if (msg.hasOwnProperty('nickName')) {
      this.nickName = msg.nickName;
    }
    if (msg.hasOwnProperty('avatarUrl')) {
      this.avatarUrl = msg.avatarUrl;
    }
    if (msg.hasOwnProperty('playLevel')) {
      this.playLevel.setPlayLevel(msg.playLevel);
    }
    if (msg.hasOwnProperty('noviceTraining')) {
      this.noviceTraining = msg.noviceTraining;
    }
    if (msg.hasOwnProperty('goldCoin')) {
      this.goldCoin = msg.goldCoin;
    }
    if (msg.hasOwnProperty('winNum')) {
      this.winNum = msg.winNum;
    }
    if (msg.hasOwnProperty('loseNum')) {
      this.loseNum = msg.loseNum;
    }
    if (msg.hasOwnProperty('tieNum')) {
      this.tieNum = msg.tieNum;
    }
    if (msg.hasOwnProperty('modifyTime')) {
      this.modifyTime = msg.modifyTime;
    }
    if (msg.hasOwnProperty('createTime')) {
      this.createTime = msg.createTime;
    }
  }
  setUserHeaderInfo(nickName: string, avatarUrl: string) {
    this.nickName = nickName;
    this.avatarUrl = avatarUrl;
  }
  showNickName() {
    return this.nickName.slice(0, 4);
  }
  showUserId() {
    return this.userId.slice(this.userId.length - 6, this.userId.length);
  }
  addGoldCoin(gc: number) {
    this.goldCoin += gc;
    if (this.goldCoin < 0) this.goldCoin = 0;
  }
  addGameNum(diffNum: number) {
    if (diffNum > 0) {
      this.winNum += 1;
    } else if (diffNum < 0) {
      this.loseNum += 1;
    } else {
      this.tieNum += 1;
    }
  }
  getGoldCoinDesc() {
    var gc = this.goldCoin;
    if (gc >= 10000) {
      gc = gc / 10000;
      const fgc = gc.toFixed(1);
      return `${fgc} 万`;
    }
    if (gc <= 0) {
      return "破产";
    }
    return `${gc}`;
  }
  // 新用户， 当天注册的用户定义为新用户
  isNewUser() {
    return new Date().toDateString() === new Date(this.createTime).toDateString();
  }
}